package com.example.searchcenter.center;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.searchcenter.R;
import com.example.searchcenter.helper.JSONParser;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

/***
 * 
 * @author NguyenTrung Function: view albums of places as single Gridview
 */

public class AlbumsFragment extends Fragment {
	// url get albums from json
	private static final String url = "http://tinhnguyen.edu.vn/apis/getAlbums";
	private ProgressDialog pDialog;
	// album id
	private String albumId = "";
	// json object
	JSONObject jsonobject;
	// json array albums
	JSONArray arrayImageJson;
	// Array list storing imageitem from json data
	List<ImageItem> arrayAlbums = new ArrayList<ImageItem>();
	private String[] arrayImage = new String[50];
	// variable for json data
	private static String TAG_AlBUMS = "albums";
	private static String TAG_ID = "id";
	private static String TAG_NAME = "name";
	private static String TAG_DES = "description";
	private static String TAG_DATE = "date";
	private static String TAG_TOTAL_IMAGE = "total";
	public static String total_image;
	private GridView gridView;
	private ProgressBar pbLoader;
	private TextView tvNotImage;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_albums, container,
				false);
		albumId = DetailsCenter.album_id;
		if(albumId.equals(null)){
			tvNotImage.setVisibility(View.VISIBLE);
			pbLoader.setVisibility(View.GONE);
			return rootView;
		}
		// Hiding the gridview and showing loader image before making the http
		// request
		gridView = (GridView) rootView.findViewById(R.id.grid_view);
		gridView.setVisibility(View.GONE);
		pbLoader = (ProgressBar) rootView
				.findViewById(R.id.pbLoader);
		pbLoader.setVisibility(View.VISIBLE);
		tvNotImage = (TextView) rootView.findViewById(R.id.tvNotImage);
		tvNotImage.setVisibility(View.GONE);
		

		
		new GetAlbums().execute();
		Log.w("array", arrayImage.toString());
		// Instance of ImageAdapter Class
		gridView.setAdapter(new ImageAdapter(getActivity(), arrayAlbums));

		/**
		 * On Click event for Single Gridview Item
		 * */
		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {

				// Sending image id to FullScreenActivity
				Intent i = new Intent(getActivity(), FullImageActivity.class);
				// passing array index
				i.putExtra("id", position);
				i.putExtra("array", arrayImage);
				startActivity(i);
			}
		});
		return rootView;
	}

	/**
	 * Background Async Task to Get complete article details
	 * */
	class GetAlbums extends AsyncTask<String, String, JSONObject> {
		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(getActivity());
			pDialog.setMessage("Đang tải ...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected JSONObject doInBackground(String... args) {
			JSONParser jsonparser = new JSONParser();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("albumId", albumId));
			// getting product details by making HTTP request
			// Note that product details url will use GET request
			jsonobject = jsonparser.makeHttpRequest(url, "GET", params);

			return jsonobject;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(JSONObject json) {
			pDialog.dismiss();
			Log.w("id_albums", albumId);
			Log.w("JSon albums", json.toString());
			try {
				total_image = json.getString(TAG_TOTAL_IMAGE);
				// arrayImage = new String[Integer.valueOf(total_image)];
				arrayImageJson = json.getJSONArray(TAG_AlBUMS);
				if(albumId !=null){
					for (int i = 0; i < arrayImageJson.length(); i++) {
						JSONObject js = arrayImageJson.getJSONObject(i);

						// Storing JSON item in a Variable
						String id = js.getString(TAG_ID);
						String name = js.getString(TAG_NAME);
						String des = js.getString(TAG_DES);
						String date = js.getString(TAG_DATE);
						ImageItem image = new ImageItem(name, id, des, date);
						arrayImage[i] = js.getString(TAG_NAME);
						arrayAlbums.add(image);
					}
					// Hide the loader, make grid visible
					pbLoader.setVisibility(View.GONE);
					gridView.setVisibility(View.VISIBLE);
				}
				else{
					
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}
}
